//
//  TabBar.swift
//  ManBetX
//
//  Created by Cocoa on 2020/3/16.
//  Copyright © 2020 Fullen. All rights reserved.
//

import UIKit

/**
 * 自定义TabBar 相关
 */

// 登录成功回调
typealias CenterButtonBlock = () -> Void

class TabBar: UITabBar {
    
// MARK: -- property
    
    var centerButtonBlock: CenterButtonBlock?
    
    lazy var customButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "tabbar_icon_center"), for: UIControl.State.normal)
        button.imageView?.contentMode = .scaleAspectFill
        button.setTitle("客服", for: UIControl.State.normal)
        button.setTitleColor(UIColor.init("929292"), for: UIControl.State.normal)
        button.titleLabel?.font = UIFont(name: "PingFangSC-Light", size: 13.0)
        button.addTarget(self, action: #selector(customButtonClick) , for: .touchUpInside)
        button.isUserInteractionEnabled = true
        return button
    }()
    
// MARK: -- func
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.isTranslucent = false
        self.tintColor = UIColor.pg_color(withHexString:"FFA855")//选中的title颜色
        self.backgroundColor = UIColor.white
        self.shadowImage = UIImage()
        self.backgroundImage = UIImage()
        
        self.addSubview(self.customButton)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        configureCustomButton()
    }
    
    // 处理中间凸出按钮不响应问题
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if self.isHidden {
            return super.hitTest(point, with: event)
        }else {
            let newPoint = self.convert(point, to: self.customButton)
            if self.customButton.point(inside: newPoint, with: event) {
                return self.customButton
            }
            return super.hitTest(point, with: event)
        }
    }
    
    // 处理 tabbar上 按钮位置
    fileprivate  func configureCustomButton() {
        let buttonWH: CGFloat = 60.0
        let width = frame.width
       
        let buttonX:CGFloat  = (width - buttonWH)/2.0
        let buttonY:CGFloat  = -12
        self.customButton.frame = CGRect(x: buttonX, y: buttonY, width: buttonWH, height: 60)
//        self.customButton.layer.masksToBounds = true
//        self.customButton.layer.cornerRadius = buttonWH/2
        
        let tabBarButtonW = width / 5;
        var tabBarButtonIndex:CGFloat = 0;

        for subview in subviews where subview is UIControl {
            if subview is UIButton {
            }else {
                
                // 设置x
                subview.frame.origin.x = tabBarButtonIndex * tabBarButtonW
                // 设置宽度
                subview.frame.size.width = tabBarButtonW;
                tabBarButtonIndex += 1;
                
//             let color =  UIColor(white: 0.5725490196078431, alpha: 1)
//                print(color)
                
            }
            // 增加索引
            if tabBarButtonIndex == 2 {
               tabBarButtonIndex += 1;
            }
        }
        // 调整中间按钮图片与标题位置
        self.customButton.imagePosition(style: UIButton.ButtonImagePosition.top, spacing: 7)
    }
   
    @objc func customButtonClick() {
        if self.centerButtonBlock != nil  {
            self.centerButtonBlock!()
        }
//        TabBarViewController.centerButtonClick()
//        print("customButtonClick")
    }
}
